package com.junlinpro.controller;


import java.nio.charset.Charset;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.junlinpro.entity.CardInfo;
import com.junlinpro.service.CollectorSetterService;
import com.junlinpro.utils.PcmInByteData;
import com.junlinpro.utils.SocketChannelMap;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("collector")
public class CollectorSetterController {

    @Autowired
    private CollectorSetterService collectorSetterService;
    
    protected HttpHeaders httpHeaders = new HttpHeaders();
    
    public CollectorSetterController() {
    	MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
        httpHeaders.setContentType(mediaType);
    }
    

    /**
               *   定时上报时间间隔修改下发
     * @param cardId
     * @param channelId
     * @param timeInterval
     * @return
     */
    @PostMapping(value = "/timeInterval")
    public ResponseEntity<String> modifyTimeInterval(@RequestBody CardInfo card) {
    	log.info("修改定时上报时间间隔接口被第三方调用，接口参数："+card);
    	
    	JSONObject result = new JSONObject();
    	result.put("code", 201);
    	result.put("data", 2);
    	result.put("msg", "接口调用失败");
        try {
            Channel channel = SocketChannelMap.get(card.getCardId() + "");            
            if (channel != null) {
                // 储存下发状态，收到嵌入式的开始监听回复后才将状态设置为true
                SocketChannelMap.taskMapPut(SocketChannelMap.startTaskKey + card.getCardId(), false);
                // 调用下发命令线程
                long timeInterval  = card.getTimeInterval()*60*1000;
                collectorSetterService.modifyTimeInterval(channel, card.getCardId(), card.getChannelId(), timeInterval);                
            }else{
  			  log.info("修改定时上报时间间隔，找不到对应的通道Channel");
  		    }

            result.put("code", 200);
        	result.put("data", 1);
        	result.put("msg", "接口调用成功");
        	
        } catch (Exception e) {
        	log.error("修改定时上报时间间隔接口被第三方调用  fail:"+e.getMessage());        	
        }
        log.info("修改定时上报时间间隔接口被第三方调用完成");
        return new ResponseEntity<String>(result.toJSONString(), httpHeaders, HttpStatus.OK);
    }

    /**
               * 修改报警参数  一级报警倍数，二级报警倍数，报警阈值(采集卡未提供此功能，该接口暂时不用)
     * @param cardId
     * @param channelId
     * @return
     * @RequestParam(value = "cardId")
     * @RequestParam(value = "channelId") 
     */
    @PostMapping(value = "/alarm")     
    public ResponseEntity<String> modifyAlarmArgs(@RequestBody CardInfo card) {
    	log.info("修改报警参数接口被第三方调用，接口参数："+card);
    	
    	JSONObject result = new JSONObject();
    	result.put("code", 201);
    	result.put("data", 2);
    	result.put("msg", "接口调用失败");
    	
        try {
            Channel channel = SocketChannelMap.get(card.getCardId() + "");

            if (channel != null) {
                // 储存下发状态，收到嵌入式的开始监听回复后才将状态设置为true
                SocketChannelMap.taskMapPut(SocketChannelMap.stopTaskKey + card.getCardId(), false);
                // 调用下发命令线程
                collectorSetterService.modifyAlarmArgs(channel, card.getCardId(), card.getChannelId(), 
                		                               card.getOneValue(),card.getTwoValue(),card.getThresholdValue());                
            }else{
  			  log.info("修改报警参数，找不到对应的通道Channel");
  		    }
            result.put("code", 200);
        	result.put("data", 1);
        	result.put("msg", "接口调用成功");
        	
        } catch (Exception e) {
        	log.error("修改报警参数接口被第三方调用  fail:"+e.getMessage());
        }
        log.info("修改报警参数接口被第三方调用  success");
        return new ResponseEntity<String>(result.toJSONString(), httpHeaders, HttpStatus.OK);
    }
    
	/**
	     * 修改数字电位器数值(采集卡未提供此功能，该接口暂时不用)
	* @param cardId
	* @param channelId
	* @return
	*/
    @PostMapping(value = "/modifyDP")
	public ResponseEntity<String> modifyDigitalPotentiometer(@RequestBody CardInfo card) {
    	log.info("修改数字电位器数值接口被第三方调用，接口参数："+card);
    	
    	JSONObject result = new JSONObject();
    	result.put("code", 201);
    	result.put("data", 2);
    	result.put("msg", "接口调用失败");
    	
		try {
		  Channel channel = SocketChannelMap.get(card.getCardId() + "");
		  if (channel != null) {
		      // 储存下发状态，收到嵌入式的开始监听回复后才将状态设置为true
		      SocketChannelMap.taskMapPut(SocketChannelMap.stopTaskKey + card.getCardId(), false);
		      // 调用下发命令线程
		      collectorSetterService.modifyDigitalPotentiometer(channel, card.getCardId(), card.getChannelId(), card.getDpValue()); 		    
		  }else{
			  log.info("修改数字电位器数值，找不到对应的通道Channel");
		  }
		    result.put("code", 200);
            result.put("data", 1);
        	result.put("msg", "接口调用成功");
		} catch (Exception e) {
			log.error("修改数字电位器数值接口被第三方调用  fail:"+e.getMessage());
		}
		log.info("修改数字电位器数值接口被第三方调用  success");
		return new ResponseEntity<String>(result.toJSONString(), httpHeaders, HttpStatus.OK);
	}  
   
}